local hx__lunshi = fk.CreateSkill {
  name = "hx__lunshi",
  tags = { Skill.Combo },
}


hx__lunshi:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    local dat = nil
    player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
      if e.id ~= player.room.logic:getCurrentEvent().id then
        local use = e.data
        if use.from == player then
          dat = use
          return true
        end
      end
    end, 1)
    if dat and dat.card.trueName == "slash" then return false end
    return target == player and player:hasSkill(hx__lunshi.name) and data.card.trueName == "slash" and
      data.extra_data and data.extra_data.combo_skill and data.extra_data.combo_skill[hx__lunshi.name]
  end,
  on_cost = function (self, event, target, player, data)
    local x = player:getMark("@hx__lunshi_count-turn")
    if player.room:askToSkillInvoke(player, {
      skill_name = hx__lunshi.name,
      prompt = "论势：是否摸"..x.."张牌并回复1点体力，然后弃置此【杀】目标及其相邻角色各一张手牌？",
    }) then
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local x = player:getMark("@hx__lunshi_count-turn")
    room:setPlayerMark(player, hx__lunshi.name, 0)
    player:drawCards( x, hx__lunshi.name)
    room:recover{
      who = player,
      num = 1,
      skillName = hx__lunshi.name,
    }
    local targets = {}
    for _, p in ipairs(data.tos) do
      if p:getLastAlive() ~= player then
        table.insert(targets, p:getLastAlive())
      end
      if p ~= player then
        table.insert(targets, p)
      end
      if p:getNextAlive() ~= player then
        table.insert(targets, p:getNextAlive())
      end
    end
    if #targets == 0 then return end
    room:doIndicate(player, targets)
    for _, p in ipairs(targets) do
      if player.dead then return end
      if not p:isKongcheng() and not p.dead then
        local card = room:askToChooseCard(player, {
          target = p,
          flag = "h",
          skill_name = hx__lunshi.name,
          prompt = "#hx__lunshi-prey::"..p.id,
        })
        room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonDiscard, hx__lunshi.name, nil, true, player)
      end
    end
  end,
})

hx__lunshi:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(hx__lunshi.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if data.card.is_damage_card then
      if player:getMark(hx__lunshi.name) > 0 and data.card.trueName == "slash" then
        data.extra_data = data.extra_data or {}
        data.extra_data.combo_skill = data.extra_data.combo_skill or {}
        data.extra_data.combo_skill[hx__lunshi.name] = true
      else
        room:setPlayerMark(player, hx__lunshi.name, 1)
      end
    else
      room:setPlayerMark(player, hx__lunshi.name, 0)
    end
  end,
})

hx__lunshi:addEffect(fk.AfterCardUseDeclared, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__lunshi.name, true) and
    player:getMark("@hx__lunshi_count-turn") < 3 and
    (data.card.trueName == "slash" or (data.card:isCommonTrick() and data.card.is_damage_card))
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@hx__lunshi_count-turn", 1)
  end,
})


return hx__lunshi
